const request = async (url, method, data) => {

    const options = {
        method: method,
        headers: {
            'Content-Type': 'application/json'
        },
    }

    if (localStorage.getItem('token')) {
        options.headers.Authorization = 'Bearer' + localStorage.getItem('token');
    }

    if (method == 'GET' || method == 'DELETE' || method == 'get' || method == 'delete') {
        let str = '';
        Object.keys(data).forEach(key => {
            str = `&${key}=${data[key]}`
        })
        url += '?' + str.slice(1);
    } else {
        options.body = JSON.stringify(data)
    }

    const res = await fetch('/api' + url, options);
    return await res.json();
}

export default request;